local skel = fk.CreateSkill {
  name = "rmt__xueqi",
  dynamic_desc = function (self, player, lang)
    local mark = player:getMark("rmt__xueqi-phase")
    if type(mark) == "table" then
      return "rmt__xueqi_inner:" .. mark[1].. ":" .. mark[2]
    end
  end,
}

Fk:loadTranslationTable{
  ["rmt__xueqi"] = "血祈",
  [":rmt__xueqi"] = "每阶段结束时，若较阶段开始时，攻击范围有你的角色数减少过/你攻击范围增加过，你可以视为使用一张【逐近弃远】/【杀】，且目标可以交给你一张牌抵消之。",

  [":rmt__xueqi_inner"] = "每阶段结束时，若较阶段开始时，攻击范围有你的角色数减少过/你攻击范围增加过，你可以视为使用一张【逐近弃远】/【杀】，且目标可以交给你一张牌抵消之<br>（本阶段开始时：攻击范围有你的角色数：{1}；攻击范围：{2}）。",

  ["#rmt__xueqi-use"] = "血祈：你可以视为使用一张指定牌",
}

skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) then
      local mark = player:getMark("rmt__xueqi-phase")
      if type(mark) ~= "table" then return false end
      local n1 = #table.filter(player.room.alive_players, function (p)
        return p:inMyAttackRange(player)
      end)
      local n2 = player:getAttackRange()
      return n1 < mark[1] or n2 > mark[2]
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local mark = player:getMark("rmt__xueqi-phase")
    if type(mark) ~= "table" then return false end
    local n1 = #table.filter(player.room.alive_players, function (p)
      return p:inMyAttackRange(player)
    end)
    local n2 = player:getAttackRange()
    local names = {}
    if n1 < mark[1] then table.insert(names, "chasing_near") end
    if n2 > mark[2] then table.insert(names, "slash") end
    local use = room:askToUseVirtualCard(player, {
      name = names, skill_name = skel.name, prompt = "#rmt__xueqi-use", skip = true,
      extra_data = { bypass_distances = true, bypass_times = true }
    })
    if use then
      table.removeOne(names, use.card.name)
      event:setCostData(self, { use = use, names = names })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    room:useCard(cost_data.use)
    local name = cost_data.names[1]
    if player.dead or not name then return end
    room:askToUseVirtualCard(player, {
      name = name, skill_name = skel.name, prompt = "#rmt__xueqi-use", skip = false,
      extra_data = { bypass_distances = true, bypass_times = true }
    })
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local n1 = #table.filter(room.alive_players, function (p)
      return p:inMyAttackRange(player)
    end)
    local n2 = player:getAttackRange()
    room:setPlayerMark(player, "rmt__xueqi-phase", {n1, n2})
  end,
})

return skel
